package cn.ctodb.cms.web.rest;

import com.codahale.metrics.annotation.Timed;
import cn.ctodb.cms.service.CmsModelService;
import cn.ctodb.cms.web.rest.util.HeaderUtil;
import cn.ctodb.cms.service.dto.CmsModelDTO;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import javax.inject.Inject;
import java.net.URI;
import java.net.URISyntaxException;
import java.util.LinkedList;
import java.util.List;
import java.util.Optional;
import java.util.stream.Collectors;
import java.util.stream.StreamSupport;

import static org.elasticsearch.index.query.QueryBuilders.*;

/**
 * REST controller for managing CmsModel.
 */
@RestController
@RequestMapping("/api")
public class CmsModelResource {

    private final Logger log = LoggerFactory.getLogger(CmsModelResource.class);
        
    @Inject
    private CmsModelService cmsModelService;

    /**
     * POST  /cms-models : Create a new cmsModel.
     *
     * @param cmsModelDTO the cmsModelDTO to create
     * @return the ResponseEntity with status 201 (Created) and with body the new cmsModelDTO, or with status 400 (Bad Request) if the cmsModel has already an ID
     * @throws URISyntaxException if the Location URI syntax is incorrect
     */
    @PostMapping("/cms-models")
    @Timed
    public ResponseEntity<CmsModelDTO> createCmsModel(@RequestBody CmsModelDTO cmsModelDTO) throws URISyntaxException {
        log.debug("REST request to save CmsModel : {}", cmsModelDTO);
        if (cmsModelDTO.getId() != null) {
            return ResponseEntity.badRequest().headers(HeaderUtil.createFailureAlert("cmsModel", "idexists", "A new cmsModel cannot already have an ID")).body(null);
        }
        CmsModelDTO result = cmsModelService.save(cmsModelDTO);
        return ResponseEntity.created(new URI("/api/cms-models/" + result.getId()))
            .headers(HeaderUtil.createEntityCreationAlert("cmsModel", result.getId().toString()))
            .body(result);
    }

    /**
     * PUT  /cms-models : Updates an existing cmsModel.
     *
     * @param cmsModelDTO the cmsModelDTO to update
     * @return the ResponseEntity with status 200 (OK) and with body the updated cmsModelDTO,
     * or with status 400 (Bad Request) if the cmsModelDTO is not valid,
     * or with status 500 (Internal Server Error) if the cmsModelDTO couldnt be updated
     * @throws URISyntaxException if the Location URI syntax is incorrect
     */
    @PutMapping("/cms-models")
    @Timed
    public ResponseEntity<CmsModelDTO> updateCmsModel(@RequestBody CmsModelDTO cmsModelDTO) throws URISyntaxException {
        log.debug("REST request to update CmsModel : {}", cmsModelDTO);
        if (cmsModelDTO.getId() == null) {
            return createCmsModel(cmsModelDTO);
        }
        CmsModelDTO result = cmsModelService.save(cmsModelDTO);
        return ResponseEntity.ok()
            .headers(HeaderUtil.createEntityUpdateAlert("cmsModel", cmsModelDTO.getId().toString()))
            .body(result);
    }

    /**
     * GET  /cms-models : get all the cmsModels.
     *
     * @return the ResponseEntity with status 200 (OK) and the list of cmsModels in body
     */
    @GetMapping("/cms-models")
    @Timed
    public List<CmsModelDTO> getAllCmsModels() {
        log.debug("REST request to get all CmsModels");
        return cmsModelService.findAll();
    }

    /**
     * GET  /cms-models/:id : get the "id" cmsModel.
     *
     * @param id the id of the cmsModelDTO to retrieve
     * @return the ResponseEntity with status 200 (OK) and with body the cmsModelDTO, or with status 404 (Not Found)
     */
    @GetMapping("/cms-models/{id}")
    @Timed
    public ResponseEntity<CmsModelDTO> getCmsModel(@PathVariable Long id) {
        log.debug("REST request to get CmsModel : {}", id);
        CmsModelDTO cmsModelDTO = cmsModelService.findOne(id);
        return Optional.ofNullable(cmsModelDTO)
            .map(result -> new ResponseEntity<>(
                result,
                HttpStatus.OK))
            .orElse(new ResponseEntity<>(HttpStatus.NOT_FOUND));
    }

    /**
     * DELETE  /cms-models/:id : delete the "id" cmsModel.
     *
     * @param id the id of the cmsModelDTO to delete
     * @return the ResponseEntity with status 200 (OK)
     */
    @DeleteMapping("/cms-models/{id}")
    @Timed
    public ResponseEntity<Void> deleteCmsModel(@PathVariable Long id) {
        log.debug("REST request to delete CmsModel : {}", id);
        cmsModelService.delete(id);
        return ResponseEntity.ok().headers(HeaderUtil.createEntityDeletionAlert("cmsModel", id.toString())).build();
    }

    /**
     * SEARCH  /_search/cms-models?query=:query : search for the cmsModel corresponding
     * to the query.
     *
     * @param query the query of the cmsModel search 
     * @return the result of the search
     */
    @GetMapping("/_search/cms-models")
    @Timed
    public List<CmsModelDTO> searchCmsModels(@RequestParam String query) {
        log.debug("REST request to search CmsModels for query {}", query);
        return cmsModelService.search(query);
    }


}
